﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Business = SoTaiChinh.Library.Business;
using Entity = SoTaiChinh.Library.Entity;
using Commom = SoTaiChinh.Library.Common;

public partial class controls_admin_adduser : System.Web.UI.UserControl
{
    Commom.Cryption.MD5Cryption md5 = new Commom.Cryption.MD5Cryption();
    private static DataTable dtThanhVien = new DataTable();
    Entity.ThanhVien entity = new Entity.ThanhVien();
    Business.ThanhVien thanhvien = new Business.ThanhVien();
    Business.QuyenTruyCap quyentruycap = new Business.QuyenTruyCap();
    DataTable dtQuyenTruyCap = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadQuyenTruyCap();
            bdpNgaySinh.Visible = false;            
            string action = Request.QueryString["action"].ToString();            
            //Nếu là thêm mới
            if (action == "add")
            {
                lbTitle.Text = "Thêm thành viên";
                bdpNgaySinh.Visible = true;
                txtNgaySinh.Visible = false;
                btnDoiNgaySinh.Visible = false;
            }
            //Nếu là sửa
            if (action == "edit")
            {
                lbTitle.Text = "Sửa thành viên";
                string info = Request.QueryString["info"].ToString();
                entity.TenDangNhap = info;
                dtThanhVien = thanhvien.SelectPrimarykey(entity);
                txtTenDayDu.Text = dtThanhVien.Rows[0]["TenDayDu"].ToString();
                txtNgaySinh.Text = dtThanhVien.Rows[0]["NgaySinh"].ToString();
                txtDiaChi.Text = dtThanhVien.Rows[0]["DiaChi"].ToString();
                txtSoDienThoai.Text = dtThanhVien.Rows[0]["SoDienThoai"].ToString();
                txtEmail.Text = dtThanhVien.Rows[0]["Email"].ToString();
                ddlQuyenTruyCap.SelectedValue = dtThanhVien.Rows[0]["QuyenTruyCap"].ToString();
                cbTrangThai.Checked = Convert.ToBoolean(dtThanhVien.Rows[0]["TrangThai"].ToString());
                txtTenDangNhap.ReadOnly = true;
                txtTenDangNhap.Text = dtThanhVien.Rows[0]["TenDangNhap"].ToString();
                txtMatKhau.TextMode = TextBoxMode.Password;
                txtMatKhau.Attributes.Add("value", dtThanhVien.Rows[0]["MatKhau"].ToString());
                txtXacNhanMatKhau.Attributes.Add("value", dtThanhVien.Rows[0]["MatKhau"].ToString());                
            }
        }
    }
    protected void LoadQuyenTruyCap()
    {
        dtQuyenTruyCap = quyentruycap.Select();
        if (dtQuyenTruyCap.Rows.Count > 0)
        {
            ddlQuyenTruyCap.DataSource = dtQuyenTruyCap;
            ddlQuyenTruyCap.DataTextField = "TenQuyen";
            ddlQuyenTruyCap.DataValueField = "Id";
        }
        else
        {
            ddlQuyenTruyCap.DataSource = null;
        }
        ddlQuyenTruyCap.DataBind();        
    }
    protected void btnQuayLai_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/admin/default.aspx");
    }
    protected void btnLuu_Click(object sender, EventArgs e)
    {
        try
        {                        
            if (Page.IsValid)
            {
                msgCaptcha.Style.Add("display", "none");
                string action = Request.QueryString["action"].ToString();
                //Nếu là sửa
                if (action == "edit")
                {
                    if (txtEmail.Text != dtThanhVien.Rows[0]["Email"].ToString() || txtTenDangNhap.Text != dtThanhVien.Rows[0]["TenDangNhap"].ToString())
                    {
                        //Kiểm tra tồn tại Email
                        entity.Email = txtEmail.Text;
                        DataTable dtExistEmail = thanhvien.ExistsEmail(entity);
                        if (dtExistEmail.Rows.Count > 0)
                        {
                            msgEmailExists.Style.Add("display", "block");
                            msgError.Style.Add("display", "block");
                            msgSuccess.Style.Add("display", "none");
                        }
                        else
                        {
                            msgEmailExists.Style.Add("display", "none");
                        }

                        //Kiểm tra tồn tại TenDangNhap
                        entity.TenDangNhap = txtTenDangNhap.Text;
                        DataTable dtExistTenDangNhap = thanhvien.ExistsTenDangNhap(entity);
                        if (dtExistTenDangNhap.Rows.Count > 0)
                        {
                            msgTenaDangNhapTonTai.Style.Add("display", "block");
                            msgError.Style.Add("display", "block");
                            msgSuccess.Style.Add("display", "none");
                        }
                        else
                        {
                            msgTenaDangNhapTonTai.Style.Add("display", "none");                            
                        }
                        if (dtExistEmail.Rows.Count == 0 && dtExistTenDangNhap.Rows.Count == 0)
                        {
                            bool save = false;
                            entity.TenDayDu = txtTenDayDu.Text;
                            if (bdpNgaySinh.Text != "")
                            {
                                entity.NgaySinh = Convert.ToDateTime(bdpNgaySinh.Text);
                            }
                            else
                            {
                                if (txtNgaySinh.Text == "")
                                {
                                    entity.NgaySinh = Convert.ToDateTime("01/01/1900"); 
                                }
                                else
                                {
                                    entity.NgaySinh = Convert.ToDateTime(txtNgaySinh.Text);
                                }
                            }
                            entity.DiaChi = txtDiaChi.Text;
                            entity.SoDienThoai = txtSoDienThoai.Text;
                            entity.Email = txtEmail.Text;
                            entity.TenDangNhap = txtTenDangNhap.Text;
                            entity.MatKhau = md5.Encrypt(txtMatKhau.Text);
                            entity.TrangThai = cbTrangThai.Checked;
                            save = thanhvien.Update(entity);

                            if (save == true)
                            {
                                msgSuccess.Style.Add("display", "block");
                                msgError.Style.Add("display", "none");
                            }
                            else
                            {
                                msgSuccess.Style.Add("display", "none");
                                msgError.Style.Add("display", "block");
                            }
                        }
                    }
                    else
                    {
                        bool save = false;
                        entity.TenDayDu = txtTenDayDu.Text;
                        entity.QuyenTruyCap =Convert.ToInt32(ddlQuyenTruyCap.SelectedValue.ToString());
                        if (bdpNgaySinh.Text != "")
                        {
                            entity.NgaySinh = Convert.ToDateTime(bdpNgaySinh.Text);
                        }
                        else
                        {
                            if (txtNgaySinh.Text == "")
                            {
                                entity.NgaySinh = Convert.ToDateTime("01/01/1900");
                            }
                            else
                            {
                                entity.NgaySinh = Convert.ToDateTime(txtNgaySinh.Text);
                            }
                        }                        
                        entity.DiaChi = txtDiaChi.Text;
                        entity.SoDienThoai = txtSoDienThoai.Text;
                        entity.Email = txtEmail.Text;
                        entity.TenDangNhap = txtTenDangNhap.Text;
                        entity.MatKhau = md5.Encrypt(txtMatKhau.Text);
                        entity.NgayThamGia = DateTime.Now;
                        entity.TrangThai = cbTrangThai.Checked;
                        save = thanhvien.Update(entity);

                        if (save == true)
                        {
                            msgSuccess.Style.Add("display", "block");
                            msgError.Style.Add("display", "none");
                        }
                        else
                        {
                            msgSuccess.Style.Add("display", "none");
                            msgError.Style.Add("display", "block");
                        }
                    }
                }
                //Nếu là thêm mới
                if (action == "add")
                {
                    //Kiểm tra tồn tại Email
                    entity.Email = txtEmail.Text;
                    DataTable dtExistEmail = thanhvien.ExistsEmail(entity);
                    if (dtExistEmail.Rows.Count > 0)
                    {
                        msgEmailExists.Style.Add("display", "block");
                        msgError.Style.Add("display", "block");
                        msgSuccess.Style.Add("display", "none");
                    }
                    else
                    {
                        msgEmailExists.Style.Add("display", "none");
                    }

                    //Kiểm tra tồn tại TenDangNhap
                    entity.TenDangNhap = txtTenDangNhap.Text;
                    DataTable dtExistTenDangNhap = thanhvien.ExistsTenDangNhap(entity);
                    if (dtExistTenDangNhap.Rows.Count > 0)
                    {
                        msgTenaDangNhapTonTai.Style.Add("display", "block");
                        msgError.Style.Add("display", "block");
                        msgSuccess.Style.Add("display", "none");
                    }
                    else
                    {
                        msgTenaDangNhapTonTai.Style.Add("display", "none");
                    }
                    if (dtExistEmail.Rows.Count == 0 && dtExistTenDangNhap.Rows.Count == 0)
                    {
                        bool save = false;
                        entity.TenDayDu = txtTenDayDu.Text;
                        entity.QuyenTruyCap = Convert.ToInt32(ddlQuyenTruyCap.SelectedValue.ToString());
                        entity.NgaySinh = Convert.ToDateTime(bdpNgaySinh.Text);
                        entity.DiaChi = txtDiaChi.Text;
                        entity.SoDienThoai = txtSoDienThoai.Text;
                        entity.Email = txtEmail.Text;
                        entity.QuyenTruyCap = Convert.ToInt32(ddlQuyenTruyCap.SelectedValue.ToString());
                        entity.TenDangNhap = txtTenDangNhap.Text;
                        entity.MatKhau = md5.Encrypt(txtMatKhau.Text);
                        entity.NgayThamGia = DateTime.Now;
                        entity.TrangThai = cbTrangThai.Checked;
                        save = thanhvien.Insert(entity);

                        if (save == true)
                        {
                            msgSuccess.Style.Add("display", "block");
                            msgError.Style.Add("display", "none");
                        }
                        else
                        {
                            msgSuccess.Style.Add("display", "none");
                            msgError.Style.Add("display", "block");
                        }
                    }
                }
            }
            else
            {
                msgCaptcha.Style.Add("display", "block");
            }            
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected void btnDoiNgaySinh_Click(object sender, EventArgs e)
    {
        bdpNgaySinh.Visible = true;
        txtNgaySinh.Visible = false;
        btnDoiNgaySinh.Visible = false;
    }
}
